import 'dart:io';

import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';

import 'widgets/window/window_caption.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  Widget buildAppBarDesktop(ColorScheme colorScheme) {
    final brightness = colorScheme.brightness;
    final backgroundColor = colorScheme.surface;
    final isDesktop =
        Platform.isWindows || Platform.isLinux || Platform.isMacOS;
    return SizedBox(
      height: isDesktop ? windowCaptionHeight : 0,
      child: FixWindowCaption(
        backgroundColor: backgroundColor,
        brightness: brightness,
        isFullScreen: false,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final virtualWindowFrameBuilder = VirtualWindowFrameInit();
    return MaterialApp(
      title: 'Flutter Demo',
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
      builder: (context, child) {
        final colorScheme = Theme.of(context).colorScheme;
        Widget childNew = Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            buildAppBarDesktop(colorScheme),
            if (child != null)
              Expanded(
                child: child,
              ),
          ],
        );
        return virtualWindowFrameBuilder(context, childNew);
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
